package com.hsd.netty.client.method;

import com.hsd.netty.client.common.NettyHeader;
import com.hsd.netty.client.common.NettyMessage;
import com.hsd.netty.client.enums.MessageType;
import com.hsd.netty.client.model.RequestObject;
import com.hsd.netty.client.utils.GsonUtils;
import com.hsd.netty.client.cache.OverallStaticCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author s.he
 * on 2020/6/8
 */
@Service
public class MethodClass {
    private static final Logger logger = LoggerFactory.getLogger(MethodClass.class);

    private static final String className = "com.hsd.netty.server.service.NettyService";

    private static final String methodName = "countNum";

    public NettyMessage getSendMessage(Integer a, Integer b) {
        NettyMessage nettyMessage = new NettyMessage();
        NettyHeader header = new NettyHeader();
        header.setSessionId(OverallStaticCache.getSessionId());
        header.setType(MessageType.REQUEST.getCode());
        RequestObject body = new RequestObject();
        body.setClassName(className);
        body.setMethodName(methodName);
        List<String> params = Arrays.asList("a", "b");
        body.setMethodParams(GsonUtils.toJson(params));
        Map<String, Object> values = new HashMap<>(2);
        values.put("a", a);
        values.put("b", b);
        body.setValues(GsonUtils.toJson(values));
        nettyMessage.setBody(GsonUtils.toJson(body));
        nettyMessage.setNettyHeader(header);

        logger.info("【接口调用请求】:{}, {}, {}, {}", className, methodName, a, b);
        return nettyMessage;
    }
}
